package me.sauce.bitcoinwallet.base

import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.view_toolbar.*
import me.sauce.bitcoinwallet.R

/**
 * @author sauce
 * @since 2018/4/3
 */

abstract class BaseActivity : AppCompatActivity() {


    open val isSimpleToolbar = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val content: View = if (isSimpleToolbar) {
            LayoutInflater.from(this)
                    .inflate(R.layout.view_toolbar, window.decorView.rootView as ViewGroup, false)
                    .apply { findViewById<FrameLayout>(R.id.content_frame).addView(injectView(), 0) }
        } else {
            injectView()
        }
        setContentView(content)

        setSupportActionBar(findViewById(R.id.toolbar))
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        initView()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    abstract fun injectView(): View

    protected open fun initView() {}
    protected fun initRId(@LayoutRes rid: Int): View {
        return LayoutInflater.from(this).inflate(rid, content_frame, false)
    }

    protected fun showProgress(isEnable: Boolean) {
        progress_bar.visibility = if (isEnable) View.VISIBLE else View.GONE
    }

}